/*
 * Copyright 2010 Yaroslav Klymko aka t3hnar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ua.t3hnar.io.serializer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ua.t3hnar.io.serializer.handlers.FieldHandler;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;

/**
 * @author Yaroslav Klymko aka t3hnar
 */
public class Serializer {
// ------------------------------ FIELDS ------------------------------

    private static final Log LOG = LogFactory.getLog(Serializer.class);
    private final SerializableFieldOwner owner;

// -------------------------- STATIC METHODS --------------------------

    public static Serializer getInstance(final SerializableFieldOwner owner) {
        return new Serializer(owner);
    }

// --------------------------- CONSTRUCTORS ---------------------------

    Serializer(final SerializableFieldOwner owner) {
        if (owner == null) {
            throw new IllegalArgumentException("Owner is null");
        }
        this.owner = owner;
    }

// -------------------------- OTHER METHODS --------------------------

    public void deserializeStreams(final ObjectInputStream inputStream) throws StreamSerializationException {
        LOG.debug("Deserializing;  inputStream: " + inputStream);
        if (inputStream == null) {
            throw new IllegalArgumentException("InputStream is null" + "");
        }

        final StreamFieldFinder finder = new DeserializeStream(inputStream);
        finder.find();
    }

    public void serializeStreams(final ObjectOutputStream outputStream) throws StreamSerializationException {
        LOG.debug("Serializing; outputStream: " + outputStream);
        if (outputStream == null) {
            throw new IllegalArgumentException("OutputStream is null");
        }

        final StreamFieldFinder finder = new SerializeFinder(outputStream);
        finder.find();
    }

// -------------------------- INNER CLASSES --------------------------

    private class SerializeFinder extends StreamFieldFinder {

        private final ObjectOutputStream outputStream;

        SerializeFinder(final ObjectOutputStream outputStream) {
            super(Serializer.this.owner);
            if (outputStream == null) {
                throw new IllegalArgumentException("OutputStream is null");
            }
            this.outputStream = outputStream;
        }

        @Override
        public void found(final SerializableFieldOwner owner, final Field field, final FieldHandler handler) throws StreamSerializationException {
            final StreamWorker<OutputStream> worker = new StreamConnector(owner, field, handler);
            worker.work(this.outputStream);
        }
    }

    private class DeserializeStream extends StreamFieldFinder {

        private final ObjectInputStream inputStream;

        DeserializeStream(final ObjectInputStream inputStream) {
            super(Serializer.this.owner);
            if (inputStream == null) {
                throw new IllegalArgumentException("InputStream is null");
            }
            this.inputStream = inputStream;
        }

        @Override
        public void found(final SerializableFieldOwner owner, final Field field, final FieldHandler handler) throws StreamSerializationException {
            final StreamWorker<InputStream> worker = new StreamSplitter(owner, field, handler);
            worker.work(this.inputStream);
        }
    }
}
